﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace LightCube
{
    class SerialPortDriver
    {
        SerialPort port;

        public SerialPortDriver()
        {
            this.port = new SerialPort();
        }

        public void Connect(string port)
        {
            this.port.PortName = port;
            this.port.Open();
            this.port.BaudRate = 115200;
        }

        public void Disconnect()
        {
            this.port.Close();
        }

        public void SetFrameRate(byte frameRate)
        {
            port.Write(new byte[] {0x01, frameRate}, 0, 2);
        }

        public void Clear()
        {
            port.Write(new byte[] { 0x11 }, 0, 1);
        }

        public void DrawFrame(byte[] frame)
        {
            port.Write(new byte[] { 0x12, (byte)frame.Length }, 0, 2);
            port.Write(frame, 0, frame.Length);
        }

        public void StartRecording()
        {
            port.Write(new byte[] { 0x21 }, 0, 1);
        }

        public void StopRecording()
        {
            port.Write(new byte[] { 0x22 }, 0, 1);
        }

        public void PlayRecording()
        {
            port.Write(new byte[] { 0x23 }, 0, 1);
        }
    }
}
